import Vue from 'vue';
import VueRouter from 'vue-router';

const Home = () => import('@/views/home/home');
const Cart = () => import('@/views/cart/cart');
const Category = () => import('@/views/category/category');
const Profile = () => import('@/views/profile/profile');
const Detail = () => import('@/views/detail/Detail');
const Login = () => import('@/views/login/Login');

// 1、安装插件
Vue.use(VueRouter);


// 2、创建路由对象
const routes = [
    {
        path: '',      // 当没有匹配到子路由时，重定向到home路由
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: Home
    },
    {
        path: '/cart',
        name: 'cart',
        component: Cart
    },
    {
        path: '/category',
        name: 'category',
        component: Category
    },
    {
        path: '/profile',
        name: 'profile',
        component: Profile
    },
    {
        path: '/detail/:iid',//动态路由
        name: 'detail',
        component: Detail
    },
    {
        path:'/login',
        name:'login',
        component:Login
    }
]

const router = new VueRouter({
    mode: 'history',
    routes
})

// 模拟登录
localStorage.setItem('token','binbin');
router.beforeEach((to, from, next)=>{
    let selfToken = localStorage.getItem('token')
        if(selfToken){
            next()
        }else if(to.path == '/profile'){
            next()
        }else{
            next({path:'/profile'})
        }
})



// 3、导出router
export default router;